package priv.abadstring.study.haoke.webapi.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import priv.abadstring.study.haoke.dubbo.entity.HouseResources;
import priv.abadstring.study.haoke.webapi.manage.HouseResourcesManage;
import priv.abadstring.study.haoke.webapi.vo.TableResult;

/**
 * HouseResourcesController
 *
 * @author aBadString
 * @date 2021-06-15 15:46:35
 */
@RestController
@RequestMapping("house/resources")
public class HouseResourcesController {
    private final HouseResourcesManage houseResourcesManage;
    public HouseResourcesController(HouseResourcesManage houseResourcesManage) {
        this.houseResourcesManage = houseResourcesManage;
    }

    /**
     * 新增房源
     * @param houseResources json数据
     */
    @PostMapping
    public ResponseEntity<Void> save(
            @RequestBody HouseResources houseResources) {
        try {
            boolean bool = this.houseResourcesManage.save(houseResources);
            if(bool){
                return ResponseEntity.status(HttpStatus.CREATED).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询房源列表
     */
    @GetMapping
    public ResponseEntity<TableResult<HouseResources>> list(
            HouseResources houseResources,
            @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        TableResult<HouseResources> tableResult = this.houseResourcesManage.list(houseResources, currentPage, pageSize);
        return ResponseEntity.ok(tableResult);
    }

    /**
     * 修改房源
     */
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody HouseResources houseResources) {
        try {
            boolean bool = this.houseResourcesManage.update(houseResources);
            if (bool) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}